package jjn.round2;

import commons.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jiang Jining
 * @since 2023-04-29 0:00
 */
public class Offer6_ReversePrintLinkedList {
    public int[] reversePrint(ListNode head) {
        List<Integer> result = new ArrayList<>();
        reverse(head, result);
        int[] finalResult = new int[result.size()];
        for (int i = 0; i < result.size(); i++) {
            finalResult[i] = result.get(i);
        }
        return finalResult;
    }
    
    private void reverse(ListNode head, List<Integer> result) {
        if (head == null) {
            return;
        }
        reverse(head.next, result);
        result.add(head.val);
    }
}
